<?php


namespace App\Http\Controllers\Order;


use App\Http\Controllers\Controller;
use App\Http\Requests\Order\Shipment\ChooseRequest;
use App\Http\Requests\Order\Shipment\PackRequest;
use App\Http\Requests\Order\Shipment\StoreRequest;
use App\Http\Searchs\Order\OrderPackSearch;
use App\Http\Searchs\Order\ShipmentSearch;
use App\Repositories\OrderShipment;
use App\Repositories\PickingDetail;
use Illuminate\Support\Facades\DB;

class ShipmentController extends Controller
{
    /**
     * @var OrderShipment
     * @author luffyzhao@vip.126.com
     */
    private $orderShipment;

    /**
     * ShipmentController constructor.
     * @param OrderShipment $orderShipment
     * @author luffyzhao@vip.126.com
     */
    public function __construct(OrderShipment $orderShipment)
    {
        $this->orderShipment = $orderShipment;
    }

    /**
     * @param ShipmentSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(ShipmentSearch $search)
    {
        return $this->response(
            $this->orderShipment->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function autoStore(StoreRequest $request)
    {
        return $this->response(
            $this->orderShipment->autoStore($request->only([
                'type', 'customer_id'
            ]))
        );
    }

    /**
     * @param ChooseRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function chooseStore(ChooseRequest $request)
    {
        return $this->response(
            $this->orderShipment->chooseStore($request->only([
                'ids'
            ]))
        );
    }

    /**
     * @param OrderPackSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function showPack(OrderPackSearch $search)
    {
        return $this->response(
            $this->orderShipment->showPack($search->toArray())
        );
    }

    /**
     * @param PackRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function postPack(PackRequest $request, $id)
    {
        return $this->response(
            DB::transaction(function () use ($request, $id) {
                return $this->orderShipment->postPack($request->only(['consumable', 'package_id']), $id);
            })
        );
    }
}
